package com.ubtechinc.cruzr.launcher2.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
import android.widget.TextView;

import com.ubtechinc.cruzr.launcher2.LauncherApplication;
import com.ubtechinc.cruzr.launcher2.R;
import com.ubtechinc.cruzr.launcher2.model.dao.FilterDao;
import com.ubtechinc.cruzr.launcher2.model.entity.AppInfo;
import com.ubtechinc.cruzr.launcher2.utlis.LogUtils;
import com.ubtechinc.cruzr.launcher2.utlis.UbtCantans;

import java.util.List;

public class SettingAdapter extends RecyclerView.Adapter<SettingAdapter.MyViewHolder> {

    private List<AppInfo> mDatas;
    private LayoutInflater mInflater;

    public interface onLastItemCallback {
        void onLastItem();
    }

    public void updateData(List<AppInfo> data) {
        mDatas = data;
        notifyDataSetChanged();
    }

    private onLastItemCallback mOnListener;

    public void setonLastItemCallback(onLastItemCallback listener) {
        this.mOnListener = listener;
    }

    public SettingAdapter(Context context, List<AppInfo> datas) {
        mInflater = LayoutInflater.from(context);
        mDatas = datas;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       return new MyViewHolder(mInflater.inflate(R.layout.item_setting, parent, false));
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final AppInfo info = mDatas.get(position);
        if(UbtCantans.POWER_CLOCK_PKG.equals(info.packagename)){
            holder.tv.setText(LauncherApplication.getContext().getString(R.string.time_date));
        }else {
            holder.tv.setText(info.appname);
        }
        holder.mSwitch.setTag(position);
        holder.mSwitch.setChecked(!info.isAdminiApp);
        // 如果使用check回调会滚动时回调，导致错位
        holder.mSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int index = (Integer) view.getTag();
                if(index >= 0 && index < mDatas.size()){
                    AppInfo oldinfo = mDatas.get(index);
                    if(oldinfo.isAdminiApp){
                        int ret = FilterDao.get().deleteAppInfo(info);
                        LogUtils.e("是否删除成功 ?= " + ret);
                        if(ret > 0){
                            oldinfo.isAdminiApp = false;
                            UbtCantans.DATA_CHANGE = true;
                        }
                    }else {
                        if(isLastItem()){
                            holder.mSwitch.setChecked(true);
                            if (mOnListener != null) {
                                mOnListener.onLastItem();
                            }
                        }else {
                            boolean ret = FilterDao.get().createOrUpdate(info);
                            LogUtils.e("是否插入成功 ?= " + ret);
                            if(ret){
                                oldinfo.isAdminiApp = true;
                                UbtCantans.DATA_CHANGE = true;
                            }
                        }
                    }
                }
            }
        });
//        // 如果设置了回调，则设置点击事件
//        if (mOnItemClickLitener != null) {
//            holder.itemView.setOnClickListener(new OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    int pos = holder.getLayoutPosition();
//                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
//                }
//            });
//
//            holder.itemView.setOnLongClickListener(new OnLongClickListener() {
//                @Override
//                public boolean onLongClick(View v) {
//                    int pos = holder.getLayoutPosition();
//                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
//                    removeData(pos);
//                    return false;
//                }
//            });
//        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }


    class MyViewHolder extends ViewHolder {
        TextView tv;
        Switch mSwitch;

        MyViewHolder(View view) {
            super(view);
            tv = (TextView) view.findViewById(R.id.appname);
            mSwitch = (Switch) view.findViewById(R.id.admini_switch);
        }
    }
    private boolean isLastItem(){
        int count = 0;
        for(AppInfo info :mDatas){
            if(!info.isAdminiApp){
                count ++;
                if(count > 1){
                    return false;
                }
            }
        }
        return true;
    }
}